<?php
require_once('constants.php');
require_once('truncate.php');
require_once('visit_count.php');
require_once('helper_functions.php');


// CONSTANTS

$per_page = 3; // nr posts per page
$pic_width = 550;
$pic_height = 350;
$pic_size = $pic_width . 'x' . $pic_height;


$user_IP = getRealIpAddr();


//connection to the database
$dbhandle=mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");


mysql_set_charset('utf8');
//select a database to work with
$selected=mysql_select_db($database, $dbhandle)
or die("Could not select examples");

$result=mysql_query("SELECT * FROM posts ORDER BY ID DESC");
$total_posts = mysql_num_rows($result);
$total_pages = floor($total_posts / $per_page);


$page = 0;
/*
$entry = $api->get_post($entryid);# Auto-correct URL
if($urltitle != $entry['rawslug'])
{
    $header = "Location: {$entry['slug']}";
    header($header);
    exit();
}
*/

if(isset($_GET['page']))
{
    $page = mysql_real_escape_string($_GET['page']);
    //echo $page . "ovo je stranica";
}

//echo $page;

$var_1 = $per_page * $page;

$limit = $per_page * $page . " ," . $per_page;

//echo $limit;
if(!isset($_GET['post_id']))
{

    //execute the SQL query and return records
    $qu = "SELECT * FROM posts ORDER BY ID DESC $limit";
    //echo($qu);
    $result=mysql_query("SELECT * FROM posts ORDER BY ID DESC LIMIT $limit");

    if(mysql_num_rows($result) == 0)
    {
        // Post with this post id does not exist so redirect user to def page blog
        header( 'Location: http://sharedserver/webizrada.me/blog' ) ;
    }

}
else
{
    $_GET['post_id'] = mysql_real_escape_string($_GET['post_id']);

    $result = mysql_query("SELECT * FROM posts WHERE ID = " . $_GET['post_id']);

    if(mysql_num_rows($result) == 0)
    {
        // Post with this post id does not exist so redirect user to def page blog
        header( 'Location: http://sharedserver/webizrada.me/blog' ) ;
    }

    $comments = mysql_query("SELECT * FROM comments WHERE posts_ID = " . $_GET['post_id']);


    get_set_count($user_IP, $_GET['post_id']);
}
//close the connection


?>

<?if(isset($_GET['post_id'])){

    // TODO blog meta tags fix

    $first_row = mysql_fetch_array($result);
    $page_title = get_string_between($first_row{'post_title'}, '<!--:me-->','<!--:-->');
    mysql_data_seek($result, 0);

    $meta['dynamic']['title']=$page_title;
    $meta['dynamic']['desc']=$page_title;
    $meta['dynamic']['keyw']="";

    //echo 'here';
}
?>



<?php include('header.php'); ?>

    <div id="center">
        <h1>Web izrada - Blog</h1>
        <div class="hr">&nbsp;</div>
        <br>
        <br>
        <div class="blog-content">

            <? while($row=mysql_fetch_array($result)): ?>
                <?

                $post_title = find_lang_in_string($row{'post_title'}, 'me');

                $html = $row{'content'};
                $start = strpos($html, '<p>');
                $end = strpos($html, '</p>', $start);
                $paragraph = substr($html, $start, $end-$start+4);
                $exp = explode('</p>',$html);
                array_splice($exp, 0, 1);
                $rest = implode('</p>', $exp);
                ?>


                <? $link_to_post="blog/" . toAscii($post_title) . '-' . $row{'ID'}?>

                <article class="post">
                    <h1><a title="<?=$post_title?>" href="<?=$link_to_post?>"><?=$post_title?></a></h1>
                    <ul class="post-metadata">
                        <li class="author">By <span>Ivan Kukić</span></li>
                        <li class="date-time"><?=date('d m Y', $row{'date_time'})?> u <?=date('H:i:s', $row{'date_time'})?>h</li>
                        <li class="visit-count">Broj posjeta: <?=$row{'visit_count'}?></li>
                        <li class="comments"><a href="<?=$link_to_post . "#comments-list"?>">Komentari</a></li>
                    </ul>

                    <?
                    $id = $row{'ID'};
                    $q =
                        "SELECT gallery.path AS path, pictures . *
                            FROM gallery
                            LEFT JOIN pictures ON gallery.ID = pictures.gallery_ID
                            WHERE gallery.posts_ID = $id
                            ORDER BY pictures.order ASC , pictures.ID ASC
                            LIMIT 1";
                    $pict_res = mysql_query($q);
                    if(mysql_num_rows($pict_res))
                    {
                        $row_pic = mysql_fetch_array($pict_res);
                        $pic_src = 'cms/pro-gallery/' . $row_pic{'path'} . '/thumbnail/' . strip_filename_extension($row_pic{'filename'}) . '_' . $pic_size . '_exacttop.' . get_filename_extension($row_pic{'filename'});
                        $inline_style = 'style="width: '. $pic_width .'px; height:' . $pic_height . 'px;"';
                        //echo $pic_src;
                    }

                    ?>

                    <?if(!isset($_GET['post_id'])):?>
                        <div class="one-post">
                            <?=$paragraph;?>
                            <?if(isset($pic_src)) echo '<a class="img_anchor" title="' . $post_title . '" href="' . $link_to_post . '"><img class="blog_img" ' . $inline_style . '" src="' . $pic_src . '"  title=" '. $post_title .' " alt="' . $post_title . '"></a>';?>

                            <?=truncateHtml($rest, 500, '...');?>
                            <a class="read-more" href="<?=$link_to_post?>">Read more</a>
                        </div>
                    <?else:?>
                        <?=$paragraph;?>
                        <?if(isset($pic_src)) echo '<img class="blog_img"' . $inline_style . 'src="' . $pic_src . '" title=" '. $post_title .' " alt="' . $post_title . '">';?>
                        <?=$rest;?>

                        <div class="article-social">
                        </div>
                    <?endif; ?>

                </article>
            <? endwhile; ?>
            <?if(isset($_GET['post_id']) && mysql_num_rows($comments)!=0):?>
            <ul id="comments-list">
                <?while($row = mysql_fetch_array($comments)):?>
                    <li class="one-comment">
                        <div class="comment-meta">
                            <img title="Avatar" alt="Avatar" src="assets/img/avatar.png"><span class="comment-author"><?=$row{'name'};?></span><span class="comment-date"><?=date('d.m.Y - h:m:s',$row{'date'});?></span>
                        </div>
                        <div class="comment-content">
                            <p><?=$row{'comment'};?></p>
                        </div>
                    </li>
                <?  endwhile;?>

            </ul>
            <?endif;?>

        </div>
        <?if(isset($_GET['post_id'])):?>
            <?php include('comment_form.php');?>
        <?endif;?>
    </div>
    <nav class="pagination">
        <?if(!isset($_GET['post_id'])):?>
            <?for($i = 0; $i < $total_pages && $total_pages != 1; $i++):?>
                <a class="one-btn <?if($i == $page) echo 'one-active'?>" href="<?echo "blog/page/". $i?>"><?echo $i+1?></a>
            <?endfor;?>
        <?endif;?>
    </nav>


    <div class="clear">&nbsp;</div>

<?mysql_close($dbhandle);?>

    <!--<div class="hr">&nbsp;</div>-->

    <br />

<?php include('footer.php'); ?>